﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace RailwayReservationWeb
{
    public partial class search_trains : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
            if (!IsPostBack)
            {
                BindStation();
            }
            if (!String.IsNullOrWhiteSpace(Request.QueryString["date"]))
            {
                DateTime day;
                var date = Request.QueryString["date"].Trim();
                if (!DateTime.TryParse(date, out day))
                {
                    Response.Redirect("~/search_trains.aspx");
                }
                var origin = Request.QueryString["origin"].Trim();
                var dest = Request.QueryString["dest"].Trim();
                var loc = RunDaysLoc(day);
                var data = db.Schedules.Where(p =>
                    p.Status == 1
                    && p.Origin == origin
                    && p.Destination == dest
                    && p.RunDays.Substring(loc, 1) == "1"
                    );
                gvwSchedule.DataSource = data;
                gvwSchedule.DataBind();
            }
            if (!IsPostBack && !String.IsNullOrWhiteSpace(Request.QueryString["origin"]))
            {
                cboOrigin.SelectedValue = Request.QueryString["origin"].Trim();
            }
            if (!IsPostBack && !String.IsNullOrWhiteSpace(Request.QueryString["dest"]))
            {
                cboDestination.SelectedValue = Request.QueryString["dest"].Trim();
            }
            if (!IsPostBack && !String.IsNullOrWhiteSpace(Request.QueryString["date"]))
            {
                txtJrnDate.Text = DateTime.Parse(Request.QueryString["date"].Trim()).ToString("MM/dd/yyyy");
            }
        }

        private void BindStation()
        {
            cboOrigin.DataSource = db.Stations.OrderBy(p => p.StationName);
            cboDestination.DataSource = db.Stations.OrderBy(p => p.StationName);
            cboOrigin.DataTextField = "StationName";
            cboOrigin.DataValueField = "StationCode";
            cboDestination.DataTextField = "StationName";
            cboDestination.DataValueField = "StationCode";
            cboOrigin.DataBind();
            cboDestination.DataBind();
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }

        public short RunDaysLoc(DateTime day)
        {
            switch (day.DayOfWeek)
            {
                case DayOfWeek.Monday: return 0;
                case DayOfWeek.Tuesday: return 1;
                case DayOfWeek.Wednesday: return 2;
                case DayOfWeek.Thursday: return 3;
                case DayOfWeek.Friday: return 4;
                case DayOfWeek.Saturday: return 5;
                case DayOfWeek.Sunday: return 6;
                default: return 0;
            }
        }

        public string FormatRunDays(string runDays)
        {
            if (runDays.Length < 7) return runDays;
            char[] dd = { 'M', 'T', 'W', 'T', 'F', 'S', 'S' };
            var sb = new StringBuilder();
            var rd = runDays.ToCharArray();
            for (int i = 0; i < 7; i++)
            {
                if (rd[i].Equals('1'))
                {
                    sb.Append(dd[i]);
                }
                else
                {
                    sb.Append("_");
                }
            }
            return sb.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            var origin = cboOrigin.SelectedValue.Trim();
            var dest = cboDestination.SelectedValue.Trim();

            DateTime day;
            var date = txtJrnDate.Text.Trim();
            if (!DateTime.TryParse(date, out day))
            {
                Response.Redirect("~/search_trains.aspx");
            }
            Response.Redirect("~/search_trains.aspx?origin=" + origin +
                "&dest=" + dest + "&date=" + day.ToString("yyyy-MM-dd").ToString());
        }

        protected void gvwSchedule_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

    }
}